package com.xtkj.superclub_friendscircle.controller;

import com.xtkj.commons.JsonParse;
import com.xtkj.commons.JsonResult;
import com.xtkj.commons.TokenUtil;
import com.xtkj.superclub_entity.entity.friendsCircle.FriendCicleShare;
import com.xtkj.superclub_entity.entity.user.User;
import com.xtkj.superclub_friendscircle.client.IUserInfoClient;
import com.xtkj.superclub_friendscircle.service.IFriendsCircleService;
import io.goeasy.GoEasy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 项目名称：superclub
 * 类 名 称：FriendCicleController
 * 类 描 述：TODO
 * 创建时间：2020/1/3 18:51
 * 创 建 人：xiezhiyan
 */
@RestController
@RequestMapping("/friendcicle")
public class FriendCicleController {
    @Autowired
    private IFriendsCircleService friendsCircleService;
    @Autowired
    private IUserInfoClient userInfoClient;


    @RequestMapping(value = "/allFriendsCircle", method = RequestMethod.GET)
    public JsonResult allFriendsCircle( @RequestParam(name = "page",required = false, defaultValue = "0") Integer page,
                                        @RequestParam(name = "size",required = false, defaultValue = "30") Integer size) {
        try {
            return friendsCircleService.allFriendsCircle(page, size);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error().setMsg("异常");
        }
    }



    @RequestMapping(value = "/sendFriendsInfo", method = RequestMethod.POST)
    public JsonResult sendFriendsInfo(HttpServletRequest request,@RequestBody FriendCicleShare friendCicleShare) {
        GoEasy goEasy = new GoEasy( "http(s)://rest-hangzhou.goeasy.io", "BC-33d23be308114797a625a3d1a9a0f4dd");
        try {
            String userId = TokenUtil.getUserId(request.getHeader("token"));
            JsonResult publisherResult = userInfoClient.getUser(new User().setUserId(userId));
            User publishUser = JsonParse.string2Obj(publisherResult.getData().toString(), User.class);
            friendCicleShare.setShareUserId(userId);
            friendsCircleService.sendFriendsInfo(friendCicleShare);
            goEasy.publish("my_channel", publishUser.getNickName()+"发布分享啦!");
            return JsonResult.success().setMsg("发送朋友圈成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error().setMsg("异常");
        }
    }

}